// 第一题
// 实现数组扁平化、去重、排序输出
let arr = [
    9,
    [1, 2],
    [3, 4, [5, 6, 7]],
    [8,0, [22, 33, 44, [55, 66]]]
];
// 递归思想
Array.prototype.flat = function() {
 const result = this.map((item) => {
     if (Array.isArray(item)) {
         return item.flat();
     };
     return [item];
 });
 return [].concat(...result);
};


Array.prototype.flat2 = function() {
    let res = this;
    while(res.some(item => Array.isArray(item))) {
        res = [].concat(...res);
    };
    return res;
}
// console.log(arr.flat());

Array.prototype.unique = function() {
    return [...new Set(this)];
}

var sortFn = function(a, b) {
    return a - b
}

console.log('res:', arr.flat().unique().sort(sortFn));
console.log('res2:', arr.flat2().unique().sort(sortFn));


